﻿/**************************************************************
 * Copyright Eastmoney Corporation. All rights reserved.
 * 
 * Author: guitao(guitao@eastmoney.com) 
 * Create Date: 2019/11/5 16:12:03
 * Description:
 *          
 * Revision History:
 *      Date         Author               Description
 *              
***************************************************************/

using gt.dotnetcore.consolesample.Biz;
using gt.dotnetcore.consolesample.Job;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Topshelf;

namespace gt.dotnetcore.consolesample
{
    public class ServiceRunner : ServiceControl
    {
        //private readonly IScheduler _scheduler;

        private IServiceProvider _serviceProvider;
        public ServiceRunner()
        {
            var configurationRoot = ConfigureConfiguration();
            _serviceProvider = ConfigureServices(configurationRoot);
        }

        private IConfiguration ConfigureConfiguration()
        {            
            //配置文件
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            return builder.Build();
        }
        private IServiceProvider ConfigureServices(IConfiguration configuration)
        {
            //依赖注入
            IServiceCollection services = new ServiceCollection();
            //后续需要使用log的话，这里需要注入
            services.AddTransient<ILoggerFactory, LoggerFactory>();
            services.AddTransient<ITest, TestBiz>();
            services.AddScoped<IJobFactory, NewJobFactory>();
            if (configuration != null)
            {
                //iconfiguration注入
                services.AddSingleton<IConfiguration>(configuration);
            }

            //自定义 option方式注入
            services.Configure<AppSetting>(configuration);
            //这里注意Job的注入方式，不要强制指定IJob实现方式！！
            services.AddScoped<TestJob>();
            services.AddScoped<Test2Job>();
            services.AddSingleton(service =>
            {
                var scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
                scheduler.JobFactory = service.GetService<IJobFactory>();
                return scheduler;
            });
            //构建容器
            return services.BuildServiceProvider();
        }

        public bool Start(HostControl hostControl)
        {
            var scheduler = _serviceProvider.GetService(typeof(IScheduler)) as IScheduler;
            scheduler.Start();
            return true;
        }

        public bool Stop(HostControl hostControl)
        {
            var scheduler = _serviceProvider.GetService(typeof(IScheduler)) as IScheduler;
            scheduler.Shutdown(true);
            return true;
        }
    }
}
